package com.ewanghuiju.app.ui.my.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseFragment;
import com.ewanghuiju.app.base.contract.my.MyContract;
import com.ewanghuiju.app.component.ImageLoader;
import com.ewanghuiju.app.model.bean.local.Bibeibean;
import com.ewanghuiju.app.model.bean.response.LbImgBean;
import com.ewanghuiju.app.model.bean.response.OrderStatusResponBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.bean.response.VersionResponBean;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.presenter.my.MyPresenter;
import com.ewanghuiju.app.ui.home.activity.InviteFriendActivity;
import com.ewanghuiju.app.ui.home.adapter.IconTitleAdapter;
import com.ewanghuiju.app.ui.main.activity.MainActivity;
import com.ewanghuiju.app.ui.my.activity.FeedbackActivity;
import com.ewanghuiju.app.ui.my.activity.KefuCenterActivity;
import com.ewanghuiju.app.ui.my.activity.MyTeamActivity;
import com.ewanghuiju.app.ui.my.activity.RedBeanHomeActivity;
import com.ewanghuiju.app.ui.my.activity.SettingActivity;
import com.ewanghuiju.app.ui.my.activity.ShouyiCenterActivity;
import com.ewanghuiju.app.ui.my.activity.WalletCenterActivity;
import com.ewanghuiju.app.ui.pintuan.activity.PintuanHotActivity;
import com.ewanghuiju.app.ui.pintuan.activity.PintuanOrderListActivity;
import com.ewanghuiju.app.ui.pintuan.activity.PintuanShopDetailsActivity;
import com.ewanghuiju.app.ui.resale.activity.ResaleHomeActivity;
import com.ewanghuiju.app.ui.resale.activity.ResaleOrderListActivity;
import com.ewanghuiju.app.ui.resale.activity.ResaleShopDetailsActivity;
import com.ewanghuiju.app.ui.watermail.activity.WaterHomeActivity;
import com.ewanghuiju.app.ui.watermail.activity.WaterShopDetailsActivity;
import com.ewanghuiju.app.ui.web.MyWebviewActivity;
import com.ewanghuiju.app.util.ClipboardUtils;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.LogUtil;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.StringUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.makeramen.roundedimageview.RoundedImageView;
import com.muddzdev.styleabletoast.StyleableToast;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.stx.xhb.androidx.XBanner;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/16
 */
public class MyFragment extends BaseFragment<MyPresenter> implements MyContract.View {

    @BindView(R.id.status_bar_view)
    View statusBarView;
    @BindView(R.id.iv_setting)
    ImageView ivSetting;
    @BindView(R.id.iv_message)
    ImageView ivMessage;
    @BindView(R.id.layiout_bar)
    RelativeLayout layioutBar;
    @BindView(R.id.iv_my_head)
    RoundedImageView ivMyHead;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.iv_vip_mark)
    ImageView ivVipMark;
    @BindView(R.id.layout_mark)
    LinearLayout layoutMark;
    @BindView(R.id.layout_yqm)
    LinearLayout layoutYqm;
    @BindView(R.id.layout_yongjin)
    LinearLayout layoutYongjin;
    @BindView(R.id.tv_ljjs)
    TextView tvLjjs;
    @BindView(R.id.layout_jsqy)
    LinearLayout layoutJsqy;
    @BindView(R.id.tv_ptdd)
    TextView tvPtdd;
    @BindView(R.id.xbanner)
    XBanner xbanner;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_invite_code)
    TextView tvInviteCode;
    @BindView(R.id.tv_my_fhd)
    TextView tvMyFhd;
    @BindView(R.id.tv_my_water)
    TextView tvMyWater;
    @BindView(R.id.tv_my_sy)
    TextView tvMySy;
    @BindView(R.id.tv_my_banlance)
    TextView tvMyBanlance;
    @BindView(R.id.tv_kted)
    TextView tvKted;
    @BindView(R.id.tv_order_ing_count)
    TextView tvOrderIngCount;
    @BindView(R.id.tv_order_success_count)
    TextView tvOrderSuccessCount;
    @BindView(R.id.tv_order_delivered_count)
    TextView tvOrderDeliveredCount;
    @BindView(R.id.tv_order_received_count)
    TextView tvOrderReceivedCount;
    @BindView(R.id.tv_order_fail_count)
    TextView tvOrderFailCount;

    private UserInfoResponBean userInfoResponBean;

    public static MyFragment newInstance() {
        Bundle bundle = new Bundle();
        MyFragment fragment = new MyFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initEventAndData() {
        initRefreshLayout();
        initBibei();
        mPresenter.taokeGetlbimg(3);
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
//        refreshLayout.autoRefresh();
        LoadingDialogUtils.show(activity);
        mPresenter.getUserInfo();
        mPresenter.getPtiorderStatus();
    }

    private void initRefreshLayout() {
        refreshLayout.setEnableRefresh(true);//是否启用下拉刷新功能
        refreshLayout.setEnableLoadMore(false);//是否启用上拉加载功能
//        refreshLayout.setEnablePureScrollMode(true);//是否启用纯滚动模式
        refreshLayout.setEnableOverScrollBounce(true);//是否启用越界回弹
        refreshLayout.setEnableOverScrollDrag(true);//是否启用越界拖动（仿苹果效果）
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(activity);
                mPresenter.getUserInfo();
                mPresenter.taokeGetlbimg(3);
                mPresenter.getPtiorderStatus();
            }
        });
    }

    /**
     * 必备工具
     */
    private void initBibei() {
        List<Bibeibean> bibeibeans = new ArrayList<>();
        bibeibeans.add(new Bibeibean("水滴商城", R.mipmap.icon_bibei_sdsc));
//        bibeibeans.add(new Bibeibean("我的钱包", R.mipmap.icon_bibei_wdqb));
//        bibeibeans.add(new Bibeibean("我的收藏", R.mipmap.icon_bibei_wdsc));
//        bibeibeans.add(new Bibeibean("浏览足迹", R.mipmap.icon_bibei_llzj));
        bibeibeans.add(new Bibeibean("邀请好友", R.mipmap.icon_bibei_yqhy));
//        bibeibeans.add(new Bibeibean("淘宝授权", R.mipmap.icon_bibei_tbsq));
//        bibeibeans.add(new Bibeibean("地推物料", R.mipmap.icon_bibei_dtwl));
        bibeibeans.add(new Bibeibean("常见问题", R.mipmap.icon_bibei_cjwt));
        bibeibeans.add(new Bibeibean("意见反馈", R.mipmap.icon_bibei_yjfk));
//        bibeibeans.add(new Bibeibean("商务合作", R.mipmap.icon_bibei_swhz));
        bibeibeans.add(new Bibeibean("客服中心", R.mipmap.icon_bibei_kfzx));
        bibeibeans.add(new Bibeibean("关于我们", R.mipmap.icon_bibei_gywm));

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        gridLayoutManager.setSmoothScrollbarEnabled(true);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setFocusable(false);
        IconTitleAdapter adapter = new IconTitleAdapter(R.layout.item_icon_title, 1);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (position) {
                    case 0:
                        new StartActivityUtil(context, WaterHomeActivity.class).startActivity(true);
//                        new StartActivityUtil(context, ResaleOrderListActivity.class).startActivity(true);
//                        new StartActivityUtil(context, ResaleShopDetailsActivity.class).startActivity(true);
                        break;
                    case 1:
                        new StartActivityUtil(context, InviteFriendActivity.class).startActivity(true);
                        break;
                    case 2:
                        new StartActivityUtil(context, MyWebviewActivity.class).putExtra("url", Constants.COMMON_PROBLEM)
                                .putExtra(Constants.WEBURL_TITLE, "常见问题").startActivity(true);
                        break;
                    case 3:
                        new StartActivityUtil(context, FeedbackActivity.class).startActivity(true);
                        break;
                    case 4:
                        new StartActivityUtil(context, KefuCenterActivity.class).startActivity(true);
                        break;
                    case 5:
                        new StartActivityUtil(context, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_ABOUTUS)
                                .putExtra(Constants.WEBURL_TITLE, "关于我们").startActivity(true);
                        break;

                    default:
                        break;
                }
            }
        });
        recyclerView.setAdapter(adapter);
        adapter.setNewData(bibeibeans);
    }

    @OnClick({R.id.tv_invite_code, R.id.iv_setting, R.id.iv_message, R.id.tv_order_more, R.id.layout_order_ing, R.id.layout_order_success, R.id.layout_order_delivered, R.id.layout_order_received, R.id.layout_order_fail,
            R.id.tv_my_team, R.id.tv_sy_order, R.id.tv_sy_report, R.id.tv_my_wallet, R.id.tv_ljjs, R.id.layout_my_sy, R.id.layout_my_banlance,
            R.id.layout_my_fhd, R.id.layout_my_water})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_invite_code:
                //复制邀请码
                if (userInfoResponBean == null || TextUtils.isEmpty(userInfoResponBean.getNumber())) {
                    StyleableToast.makeText(context, "订单号为空", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                ClipboardUtils.copyText(userInfoResponBean.getNumber(), "复制成功");
                break;
            case R.id.iv_setting:
                //设置
                new StartActivityUtil(context, SettingActivity.class).startActivity(true);
                break;
            case R.id.iv_message:
                //消息

                break;
            case R.id.tv_order_more:
                //更多订单
                new StartActivityUtil(context, PintuanOrderListActivity.class).putExtra(Constants.PINTUANORDER_TYPE, 0).startActivity(true);
                break;
            case R.id.layout_order_ing:
                //正在拼团
                new StartActivityUtil(context, PintuanOrderListActivity.class).putExtra(Constants.PINTUANORDER_TYPE, 1).startActivity(true);
                break;
            case R.id.layout_order_success:
                //成功拼团
                new StartActivityUtil(context, PintuanOrderListActivity.class).putExtra(Constants.PINTUANORDER_TYPE, 2).startActivity(true);
                break;
            case R.id.layout_order_delivered:
                //待发货
                new StartActivityUtil(context, PintuanOrderListActivity.class).putExtra(Constants.PINTUANORDER_TYPE, 3).startActivity(true);
                break;
            case R.id.layout_order_received:
                //待收货
                new StartActivityUtil(context, PintuanOrderListActivity.class).putExtra(Constants.PINTUANORDER_TYPE, 4).startActivity(true);
                break;
            case R.id.layout_order_fail:
                //失败拼团
                new StartActivityUtil(context, PintuanOrderListActivity.class).putExtra(Constants.PINTUANORDER_TYPE, 6).startActivity(true);
                break;
            case R.id.tv_my_team:
                //我的团队
                new StartActivityUtil(context, MyTeamActivity.class).startActivity(true);
                break;
            case R.id.tv_sy_order:
                //收益订单

                break;
            case R.id.layout_my_sy:
            case R.id.tv_sy_report:
                //收益报表
                new StartActivityUtil(context, ShouyiCenterActivity.class).startActivity(true);
                break;
            case R.id.layout_my_banlance:
            case R.id.tv_my_wallet:
                //我的钱包
                new StartActivityUtil(context, WalletCenterActivity.class).startActivity(true);
                break;
            case R.id.tv_ljjs:
                //立即转化 跳转到会员中心
                ((MainActivity) activity).jumpToVip();
                break;
            case R.id.layout_my_fhd:
                //分红豆
                new StartActivityUtil(context, RedBeanHomeActivity.class).putExtra(Constants.WATER_REDBEAN_TYPE, 1).startActivity(true);
                break;
            case R.id.layout_my_water:
                //水滴
                new StartActivityUtil(context, RedBeanHomeActivity.class).putExtra(Constants.WATER_REDBEAN_TYPE, 2).startActivity(true);
                break;
            default:
                break;
        }


    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void showContent(String list) {

    }

    @Override
    public void showUserInfo(UserInfoResponBean bean) {
        LoadingDialogUtils.dismissDialog_ios();
        if (refreshLayout != null)
            refreshLayout.finishRefresh();

        if (bean != null) {
            this.userInfoResponBean = bean;
            String nickName = StringUtil.getLengthTextByStr(TextUtils.isEmpty(bean.getNickname()) ? bean.getMobile() : bean.getNickname(), 4);
            tvNickname.setText(nickName);
            tvInviteCode.setText(TextUtils.isEmpty(bean.getNumber()) ? "" : "邀请码：" + bean.getNumber());
            tvMyFhd.setText(TextUtils.isEmpty(bean.getRed_bean()) ? "" : bean.getRed_bean());
            tvMyWater.setText(TextUtils.isEmpty(bean.getIntegral()) ? "" : bean.getIntegral());
            tvMySy.setText(TextUtils.isEmpty(bean.getTotal()) ? "0.00" : bean.getTotal());
            tvMyBanlance.setText(TextUtils.isEmpty(bean.getBalance()) ? "" : bean.getBalance());

            tvKted.setText("开团额度(元) " + (TextUtils.isEmpty(bean.getGroup_integral()) ? "" : bean.getGroup_integral()));
//            ImageLoader.load(context, bean.getHeadimg(), ivMyHead);
            Glide.with(activity).load(bean.getHeadimg()).placeholder(R.mipmap.icon_default_head).error(R.mipmap.icon_default_head).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(ivMyHead);

            // 0: 普通 1:团长 2:代理 3: 区域代理 4: 销售
            switch (bean.getIs_daili()) {
                case 0:
                    ImageLoader.load(context, R.mipmap.vip_common_mark, ivVipMark);
                    break;

                case 1:
                    ImageLoader.load(context, R.mipmap.icon_tuanzhang_mark, ivVipMark);
                    break;

                case 2:
                    ImageLoader.load(context, R.mipmap.vip_qz_mark, ivVipMark);
                    break;

                case 3:
                    ImageLoader.load(context, R.mipmap.icon_area_daili_mark, ivVipMark);
                    break;
            }
        }

    }

    @Override
    public void showUserInfoError() {
        if (refreshLayout != null)
            refreshLayout.finishRefresh();
    }

    @Override
    public void settingAvaterSuccess() {

    }

    @Override
    public void bindWxSuccess() {

    }

    @Override
    public void unBindWxSuccess() {

    }

    @Override
    public void showOsstoken(BaseResponse baseResponse) {

    }

    @Override
    public void showLbImg(int type, List<LbImgBean> list) {
        //广告轮播
        if (list != null && list.size() > 0) {
//            xbanner.setBannerData(R.layout.banner_item_with_corner, list);
            xbanner.setVisibility(View.VISIBLE);
            xbanner.setBannerData(list);
            xbanner.loadImage(new XBanner.XBannerAdapter() {
                @Override
                public void loadBanner(XBanner banner, Object model, View view, int position) {
                    ImageView iv = (ImageView) view;
                    iv.setScaleType(ImageView.ScaleType.FIT_XY);
                    Glide.with(context).load(list.get(position).getSrc()).dontAnimate().into(iv);
                }
            });
            xbanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
                @Override
                public void onItemClick(XBanner banner, Object model, View view, int position) {
                    try {
                        LbImgBean lbImgBean = list.get(position);
                        if (lbImgBean == null) {
                            return;
                        }
                        if (lbImgBean.getGoods_type() == 1) {
                            //一级分类
                            new StartActivityUtil(context, PintuanHotActivity.class).putExtra(Constants.PINTUAN_SHOP_TYPE,
                                    3).putExtra(Constants.PINTUAN_SHOP,
                                    lbImgBean.getGoods_id()).putExtra(Constants.PINTUAN_SHOP_TITLE,
                                    TextUtils.isEmpty(lbImgBean.getGoods_name()) ? "商品" : lbImgBean.getGoods_name())
                                    .startActivity(true);
                        } else if (lbImgBean.getGoods_type() == 2) {
                            //二级分类
                            new StartActivityUtil(context, PintuanHotActivity.class).putExtra(Constants.PINTUAN_SHOP_TYPE,
                                    2).putExtra(Constants.PINTUAN_SHOP,
                                    lbImgBean.getGoods_id())
                                    .putExtra(Constants.PINTUAN_SHOP_TITLE,
                                            TextUtils.isEmpty(lbImgBean.getGoods_name()) ? "商品" : lbImgBean.getGoods_name())
                                    .startActivity(true);
                        } else if (lbImgBean.getGoods_type() == 3) {
                            //商品id
                            new StartActivityUtil(activity, PintuanShopDetailsActivity.class).putExtra(Constants.PINTUAN_SHOP_ID,
                                    lbImgBean.getGoods_id())
                                    .startActivity(true);

                        } else if (lbImgBean.getGoods_type() == 4) {
                            //水滴商城
                            new StartActivityUtil(activity, WaterShopDetailsActivity.class).putExtra(Constants.WATER_SHOP_ID,
                                    lbImgBean.getGoods_id())
                                    .startActivity(true);

                        } else {
                            if (!TextUtils.isEmpty(lbImgBean.getJump())) {
                                new StartActivityUtil(activity, MyWebviewActivity.class).putExtra("url", lbImgBean.getJump())
                                        .putExtra(Constants.WEBURL_TITLE, lbImgBean.getTitle()).startActivity(true);
                            }
                        }
//                        LbImgBean lbImgBean = list.get(position);
//                        if (lbImgBean == null || TextUtils.isEmpty(lbImgBean.getJump())) {
//                            return;
//                        }
//                        new StartActivityUtil(activity, MyWebviewActivity.class).putExtra("url", lbImgBean.getJump())
//                                .putExtra(Constants.WEBURL_TITLE, lbImgBean.getTitle()).startActivity(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        } else {
            xbanner.setVisibility(View.GONE);
        }
    }

    @Override
    public void showUpgradeInfo(VersionResponBean versionResponBean) {

    }

    @Override
    public void showPtiorderStatus(OrderStatusResponBean orderStatusResponBean) {
        if (orderStatusResponBean != null) {
            //拼团中
            if (orderStatusResponBean.getPintuanzhong() > 0) {
                tvOrderIngCount.setVisibility(View.VISIBLE);
                StringUtil.getTipByNum(tvOrderIngCount, orderStatusResponBean.getPintuanzhong());

            } else {
                tvOrderIngCount.setVisibility(View.GONE);
            }
            //待发货
            if (orderStatusResponBean.getDaifa() > 0) {
                tvOrderDeliveredCount.setVisibility(View.VISIBLE);
                StringUtil.getTipByNum(tvOrderDeliveredCount, orderStatusResponBean.getDaifa());
            } else {
                tvOrderDeliveredCount.setVisibility(View.GONE);
            }
            //待收货
            if (orderStatusResponBean.getDaishou() > 0) {
                tvOrderReceivedCount.setVisibility(View.VISIBLE);
                StringUtil.getTipByNum(tvOrderReceivedCount, orderStatusResponBean.getDaishou());
            } else {
                tvOrderReceivedCount.setVisibility(View.GONE);
            }

        }
    }
}

